<template>
  <div class="app-container">
    <el-form
      :model="formData"
      ref="formData"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="黑名单类型：" prop="dictLabel">
        <el-select v-model="formData.blackType" placeholder="请选择" clearable>
          <el-option
            v-for="item in blackList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customerService:blacklistThesaurus:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="openImport"
          v-hasPermi="['customerService:blacklistThesaurus:import']"
          >导入</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格   -->
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column label="关键词" align="center" prop="keywordContent" />
      <el-table-column label="黑名单类型" align="center" prop="blackType" />
      <el-table-column label="命中次数" align="center" prop="hitCounts" />
      <el-table-column
        label="会话时间"
        align="center"
        prop="createTime"
      ></el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />

    <importCommon
      v-if="importShow"
      :importShow="importShow"
      :uploadUrl="'/system/sysBlackKeyword/importData'"
      :downloadUrl="'system/sysBlackKeyword/importTemplate'"
      @onImportCancel="onImportCancel"
    ></importCommon>
  </div>
</template>

<script>
import { getList } from "@/api/customerService/blacklist-thesaurus";
import { listData } from "@/api/system/dict/data";
import importCommon from "../components/import-common.vue";

export default {
  name: "sessionList",
  components: { importCommon },
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      showSearch: true, //显示搜索条件
      // 查询参数
      formData: {
        blackType: "", //黑名单类型
      },
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      // 黑名单类型列表
      blackList: [],
      dateRange: [], //日期范围
      total: 0,
      importShow: false, //导入
    };
  },
  async created() {
    await this.getListType();
    await this.getList();
  },
  methods: {
    // 黑名单类型列表
    async getListType() {
      const { rows } = await listData({
        pageNum: 1,
        pageSize: 100,
        dictType: "sys_keyword_list",
      });
      this.blackList = rows || [];
    },

    // 列表查询
    async getList() {
      const data = this.formData.blackType &&  this.blackList.find(
        (el) => el.dictValue === this.formData.blackType
      );
      let params = {
        blackType:data?.dictLabel || '',
        ...this.pagination,
      };
      this.loading = true;
      const { total, rows } = await getList(
        this.addDateRange(params, this.dateRange)
      );
      this.total = total;
      this.tableData = rows;
      this.loading = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.formData = {
        blackType: "", //黑名单类型
      };
      this.dateRange = [];
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
        let typeAll = this.formData.blackType &&  this.blackList.find(
        (el) => el.dictValue === this.formData.blackType
      );
      const url = `system/sysBlackKeyword/exportBlackLog`;
      let data = {
        blackType:typeAll?.dictLabel || '',
      };
      if (this.dateRange?.length) {
        data.params =
          typeof data.params === "object" &&
          data.params !== null &&
          !Array.isArray(data.params)
            ? data.params
            : {};
        data.params["beginTime"] = this.dateRange[0];
        data.params["endTime"] = this.dateRange[1];
      }
      this.download(
        url,
        {
          ...data,
        },
        `sysBlackKeyword_${new Date().getTime()}.xlsx`
      );
    },

    //   导入
    openImport() {
      this.importShow = true;
    },

    // 导入关闭弹窗
    onImportCancel(){
        this.importShow = false; 
        this.getList()
    }
  },
};
</script>

<style lang="scss" scoped></style>
